<?php

class Regions_model extends MY_CI_Model
{
    public function __construct()
    {
        $this->table 		= 'regions';
		$this->primary_id 	= 'region_id';
		$this->primary_name	= 'region_name';

		parent::__construct ();
    }
    /**
     * 查询条件
     * @param array $filter
     * @return void
     */
    function list_query_filter($filter = array())
    {
        if(isset($filter['keywords']) && !empty($filter['keywords'])) {
            $where = '(a.'.$this->primary_name . ' like "%' . $filter['keywords'] . '%" ' . ' or a.full_spell like "%' . $filter['keywords'] . '%"' .  'or a.full_spell_initial like "%' .$filter['keywords'] . '%")';
            $this->db->where($where);
        }
        if(isset($filter['parent_id']) && !empty($filter['parent_id'])) {
        	$this->db->where('a.parent_id', $filter['parent_id']);       
        }
        if(isset($filter['is_open']) && $filter['is_open'] > 0){
    		$this->db->where('a.is_open', $filter['is_open'] - 10);
    	}
    }
    /**
     * 获取列表
     * @param array $filter
     * @param array $pager
     * @return multitype:Ambigous <multitype:, number> multitype:string
     */
    public function query_item_list($filter, $pager)
    {
    	$this->db->select('a.*');
    	$this->db->from($this->table . ' as a');
    	
    	$this->list_query_filter($filter);
    	
    	$this->db->order_by('a.municipality', 'desc');
    	$this->db->limit($pager['page_size'], $pager['start']);
    	$item_list = $this->db->get()->result_array();
    	return $item_list;
    }
    /**
     * 格式化输出字段
     * @param unknown $item_list
     * @return multitype:unknown
     */
    function reformat_list_datas($item_list)
    {
    	$arr = array();
    	foreach ($item_list as $row) {
    		$row['region_name_urlencode'] = rawurlencode($row['region_name']);
    		$arr[] = $row;
    	}
    	return $arr;
    }
    function get_region_info($type, $region_id = 0)
    {
        $tab_name = $type == 1 ? 'province' : ($type == 2 ? 'city' : 'area');
		$this->db->select('*');
		$this->db->from($tab_name);
		$this->db->where('code', $region_id);
		$info = $this->db->get()->row_array();
		return $info['name'];
    }
    /**
     * 获得指定国家的所有省
     *
     * @access      public
     * @param       int     country    国家的编号 具体ID
     * @return      array
     */
    function get_province()
    {
        $this->db->select('id, code, name');
        $this->db->from('province');
        $regions = $this->db->get()->result_array();
        return $regions;
    }
        /**
     * 获得指定省的所有市
     *
     * @access      public
     * @param       int     country    国家的编号 具体ID
     * @return      array
     */
    function get_city($province_id)
    {
        if($province_id > 0){
            $this->db->select('id, code, name');
            $this->db->from('city');
            $this->db->where('provincecode', $province_id);
            $regions = $this->db->get()->result_array();
            return $regions;
        }
        
    }
            /**
     * 获得指定市的所有区
     *
     * @access      public
     * @param       int     country    国家的编号 具体ID
     * @return      array
     */
    function get_area($province_id)
    {
        if($province_id > 0){
            $this->db->select('id, code, name');
            $this->db->from('area');
            $this->db->where('citycode', $province_id);
            $regions = $this->db->get()->result_array();
            return $regions;
        }
        
    }
    
    
    
    
    function region_list($province = 0, $city = 0, $district = 0)
    {
    	$regions = array('province' => array(), 'city' => array(), 'district' => array());

    	$regions['province'] = $this->get_province();
        
    	if ($province > 0) {
            $regions['city'] = $this->get_city($province);            
            if($city > 0){
                $regions['district'] = $this->get_area(2);
            }
    	}
    	
    	return $regions;
    }
    
}

?>